﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CsProjEditor;
using CheckBox = System.Windows.Controls.CheckBox;
using MessageBox = System.Windows.MessageBox;

namespace EditorCsProj
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string folderPath = "";

        private List<string> filePaths = new List<string>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnChoiceFolder_Click(object sender, RoutedEventArgs e)
        {
            uniformGrid.Children.Clear();
            FolderBrowserDialog fd = new FolderBrowserDialog();
            DialogResult dialogResult = fd.ShowDialog();
            if (dialogResult != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            folderPath= fd.SelectedPath;
            txtPath.Text = folderPath;
            DirectoryInfo directory= new DirectoryInfo(folderPath);
            var files = directory.GetFiles("*.csproj", System.IO.SearchOption.AllDirectories);
            //var files=  Directory.GetFiles(folderPath);
            foreach (var file in files)
            {
                try
                {
                    CsProjEditor.Project project = Project.Load(file.FullName);
                    bool existsNode = project.ExistsNode("PropertyGroup", "VersionPrefix");
                    if (!existsNode)
                    {
                        continue;
                    }

                    var c = project.GetNodeValue("PropertyGroup", "VersionPrefix");

                    if (c == null)
                    {
                        return;
                    }

                    if (c.Length == 0)
                    {
                        return;
                    }

                    string versionPrefix = c[0];

                    CheckBox chk = new CheckBox();
                    chk.Content = file.Name;
                    chk.ToolTip = file.FullName;
                    chk.Tag= file.FullName;
                    chk.IsChecked = true;
                    TextBlock txt = new TextBlock();
                    txt.Text= versionPrefix;
                    uniformGrid.Children.Add(chk);
                    uniformGrid.Children.Add(txt);
                }
                catch (Exception exception)
                {
                    
                }
            }

        }


        private void BtnUpdate_OnClick(object sender, RoutedEventArgs e)
        {
            foreach (UIElement uniformGridChild in uniformGrid.Children)
            {
                try
                {
                    if (uniformGridChild is CheckBox checkBox)
                    {
                        CsProjEditor.Project project = Project.Load(checkBox.Tag.ToString());
                        var c = project.GetNodeValue("PropertyGroup", "VersionPrefix");

                        if (c == null)
                        {
                            return;
                        }

                        if (c.Length == 0)
                        {
                            return;
                        }

                        string versionPrefix = c[0];
                        project.SetNodeValue("PropertyGroup", "VersionPrefix", c[0], txtVersionPrefix.Text);
                        project.Save(checkBox.Tag.ToString());
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                    return;
                }
                
            }
        }
    }
}
